import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: undefined,
    userinfo: {
      name: "",
      role: "",
      team: "",
      account: ""
    },
  },
  getters: {
    token: (state) => state.token,
    islogin: (state) => !!state.token,
    userinfo: (state) => state.userinfo || {},
  },
  mutations: { //写在里面的主要用于改变数据
    // login: (state, data) => {
    //   state.userData = data.data;
    //   localStorage['power'] = data.data ? data.data.power : 'admin';
    // },
    login: (state, data) => {
      state.token = data.token;
      state.userinfo.name = data.userinfo.name;
      state.userinfo.role = data.userinfo.role;
      state.userinfo.id = data.userinfo.id;
      state.userinfo.account = data.userinfo.account;
    },
    logout: (state) => {
      state.token = undefined;
      state.userinfo.name = "";
      state.userinfo.role = "";
      state.userinfo.id = "";
      state.userinfo.account = "";
    },
  }
});
